package com.ubxtech.mqtt.service.impl;

import com.ubxtech.mqtt.config.MqttBeanHolderUtil;
import com.ubxtech.mqtt.config.MqttConstant;
import com.ubxtech.mqtt.service.EmqTopicManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.integration.mqtt.inbound.AbstractMqttMessageDrivenChannelAdapter;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * @author Victor.Xiao
 * @since 2022-03-30 11:00
 **/
@Slf4j
@Component
public class EmqTopicManagerImpl implements EmqTopicManager {

    /**
     * 根据通道获取adapter
     * @author Victor.Xiao
     * @since 2022-11-10 11:31
     * @return org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter
     */
    private AbstractMqttMessageDrivenChannelAdapter getAdapter() {
        boolean hasDefault = MqttBeanHolderUtil.hasAdapter(MqttConstant.DEFAULT_CHANNEL_NAME);
        if (hasDefault) {
            return getAdapter(MqttConstant.DEFAULT_CHANNEL_NAME);
        }
        AbstractMqttMessageDrivenChannelAdapter adapter = MqttBeanHolderUtil.getDefaultAdapter();
        if (adapter == null) {
            log.error("没有adapter");
            throw new IllegalArgumentException("没有adapter");
        }
        return adapter;
    }

    /**
     * 根据通道获取adapter
     * @author Victor.Xiao
     * @since 2022-11-10 11:31
     * @param channelName channelName
     * @return org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter
    */
    private AbstractMqttMessageDrivenChannelAdapter getAdapter(String channelName) {
        AbstractMqttMessageDrivenChannelAdapter adapter = MqttBeanHolderUtil.getAdapter(channelName);
        if (adapter == null) {
            log.error("没有找到{}的adapter", channelName);
            throw new IllegalArgumentException("没有找到" + channelName + "的adapter");
        }
        return adapter;
    }


    @Override
    public void addListenTopic(String topic) {
        addListenTopic(topic,1);
    }

    @Override
    public void addListenTopic(String topic,int qos) {

        try {
            getAdapter().addTopic(topic, qos);
        } catch (Exception e) {
            log.error("添加主题有误",e);
            e.printStackTrace();
        }
    }

    @Override
    public void removeListenTopic(String topic) {
        getAdapter().removeTopic(topic);
    }

    @Override
    public List<String> getTopicList() {
        return new ArrayList<>(Arrays.asList(getAdapter().getTopic()));
    }

    @Override
    public void addListenTopic(String channel, String topic) {
        addListenTopic(channel,topic,1);
    }

    @Override
    public void addListenTopic(String channel, String topic, int qos) {
        try {
            getAdapter(channel).addTopic(topic, qos);
        } catch (Exception e) {
            log.error("添加主题有误",e);
            e.printStackTrace();
        }
    }

    @Override
    public void removeListenTopic(String channel, String topic) {
        getAdapter(channel).removeTopic(topic);
    }

    @Override
    public List<String> getTopicList(String channel) {
        return new ArrayList<>(Arrays.asList(getAdapter(channel).getTopic()));
    }

}
